package cur.nature.admin.common.config;

import cur.nature.framework.core.log.LOG;
import cur.nature.framework.core.util.StringUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.util.Arrays;
import java.util.stream.Collectors;

@Configuration
@ConditionalOnProperty(value = {"cur.component.admin.enable"}, havingValue = "true", matchIfMissing = true)
//https://blog.csdn.net/daweicheng/article/details/134850266
public class LocalMessageConfig {

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        String[] strings = scanModelsForI18nFolders();
        messageSource.setBasenames(strings);
        messageSource.setDefaultEncoding("UTF-8");
        LOG.info("加载messageSource："+ StringUtil.join(",", Arrays.stream(strings).map(String::toString).collect(Collectors.toList())));
        return messageSource;
    }

//    @Bean
//    public LocaleResolver localeResolver() {
//        CookieLocaleResolver resolver = new CookieLocaleResolver();
//        // 默认语言
//        resolver.setDefaultLocale(new Locale("en","US"));
//        resolver.setCookieName("language");
//        resolver.setCookieMaxAge(3600); // 1 hour
//        return resolver;
//    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        // 整合国际化
//        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
//        interceptor.setParamName("lang");
//        registry.addInterceptor(interceptor);
//    }

    public String[] scanModelsForI18nFolders() {
        try {
            // 通配规则 直接获取 Resources中的内容
//            String resourcePattern = "classpath*:i18n/*_messages.properties";
            String resourcePattern = "classpath*:i18n/*.properties";
            ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
            Resource[] resources = resourcePatternResolver.getResources(resourcePattern);
            String[] paths = new String[resources.length];

            for (int i = 0; i < resources.length; i++) {
                Resource resource = resources[i];
                String[] urlStrArr = resource.getURL().toString().split("/");
                int index = urlStrArr.length - 1;
                String moduleName = urlStrArr[index];
                // 通配符加+ 模块名 + /国际化数据目录
                paths[i] = "classpath:i18n/" + moduleName.substring(0, moduleName.length() - 11);

            }
            return paths;

        } catch (Exception e) {

        }
        return null;
    }
}

